<?php
/*
 * Author :pandiam
 */
session_start();
require_once ('../BL/BL.php');
require_once ('include/functions.php');

if (!isset($_SESSION['uid']) || $_SESSION['uid'] == -1) {
    UI_registerError('msg_warning', "You are not logged in.");
    header('Location: login.php');
    exit(0);
}

if ( !isset($_POST['data']) ) {
    UI_registerError('msg_warning', "You haven't selected an entity to delete!");
    UI_registerError('msg_warning', "Please select a category from the sidebar to the left.");
    header("location: profile.php");
    exit(0);
}
else
{
    //print_r($_POST['data']);
    //die;
    $ids = split(",",$_POST['data']);
    $entities = BL_selectCategory($_POST['catId']);
    $deleteData = array();
    $k=0;
    for($i=0;$i<count($entities);$i++){
       for($j=0;$j<count($ids);$j++){
            if(strcmp($entities[$i]->getName(),$ids[$j])==0){
                break;      
            }
       }
       if($j==count($ids)){
            $deleteData[$k] = $entities[$i]->getId() ;
       }
    }
    
    if (  ($res = BL_delete_entities($_POST['catId'],$deleteData,$_SESSION['uid'])) < 0 )
    {
        BL_registerError($res);
        BL_flusherrors();
    }
    else {
        UI_registerError("msg_success", "Successfully deleted entity.");
    }
    header("location: profile.php");
    exit(0);
}
?>